﻿package controllers.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.User;
import utils.mySqlUtil;

import models.Esgridcfg;
import models.Estate2;
import models.Estate2other;
import models.Estate2gd;
import models.EstateQuery;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.JspView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class OtherController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
    public void index1() throws Exception{
    	User user = (User) rbac.getCurrentUser();
    	List<Esgridcfg> fields = Esgridcfg.findAll(Esgridcfg.class," uid = '" + user.getLogin() + "' and isshow = 1",null,"sort");
		request.setAttribute("fields", fields);	
		request.setAttribute("fieldscount", fields.size());	
	}

    public void index2() throws Exception{
    	User user = (User) rbac.getCurrentUser();
    	List<Esgridcfg> fields = Esgridcfg.findAll(Esgridcfg.class," uid = '" + user.getLogin() + "' and isshow = 1",null,"sort");
		request.setAttribute("fields", fields);	
		request.setAttribute("fieldscount", fields.size());	
    }
    
    public JspView showOtherEstateInfo(int id) throws Exception {
			
		Estate2gd item = null;
		
        item = Estate2gd.find(Estate2gd.class,id);
        
		
        
        JspView view = new JspView("/read/esinfo/showEstateInfo.jsp"); 
        view.setAttribute("item", item);
        view.setAttribute("rm", "norm");
        return view;
	}
    
    public JspView showOtherEstateInfo1(int id) throws Exception {
		
		Estate2other item = null;
		
        item = Estate2other.find(Estate2other.class,id);
        
		
        JspView view = new JspView("/read/esinfo/showEstateInfo.jsp"); 
        view.setAttribute("item", item);
        view.setAttribute("rm", "norm");
        return view;
	}
	
	public View getEstate2Items(EstateQuery estateQuery,int page, int rows, String q,int t, String sort, String order) throws Exception {
		if (page == 0) page = 1;
		if (rows == 0) rows = 50;
		User user = (User) rbac.getCurrentUser();		
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = mySqlUtil.getEstateQueryString(estateQuery,user.getDepartment().getCompany_id());
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		**/
		if (q != null){
			cond += " and (address like '%"+q+"%' or mode like '%"+q+"%' ) ";			
		}
		
		//if (!isAdmin())
        //    cond += " and z_id = '" + user.getDepartment().getCompany_id() +"'";

		Object[] args = tmpArgs.toArray();
		
		long total = Estate2gd.count(Estate2gd.class, cond, args);
		List<Estate2gd> items = Estate2gd.findAll(Estate2gd.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View getEstate2Items1(EstateQuery estateQuery,int page, int rows, String q,int t, String sort, String order) throws Exception {
		if (page == 0) page = 1;
		if (rows == 0) rows = 50;
		User user = (User) rbac.getCurrentUser();		
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = mySqlUtil.getEstateQueryString(estateQuery,user.getDepartment().getCompany_id());
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		**/
		if (q != null){
			cond += " and (address like '%"+q+"%' or mode like '%"+q+"%' ) ";			
		}
		
		//if (!isAdmin())
        //    cond += " and z_id = '" + user.getDepartment().getCompany_id() +"'";

		Object[] args = tmpArgs.toArray();
		
		long total = Estate2other.count(Estate2other.class, cond, args);
		List<Estate2other> items = Estate2gd.findAll(Estate2other.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	
	
}
